#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>

const int nums = 10;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *wait(void *args)
{
    std::string name = static_cast<const char*>(args);
    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        usleep(10000);
        std::cout << "I am " << name << std::endl;
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{

    pthread_t threads[nums];
    // 创建多线程
    for (int i = 0; i < nums; i++)
    {
        char *name = new char[128];
        snprintf(name, 128, "thread'name - %d", i + 1);
        pthread_create(&threads[i], nullptr, wait, (void *)name);
        usleep(10000);
    }
    // 唤醒线程
    while (true)
    {
        pthread_cond_broadcast(&cond);
        std::cout << "thread wake up ... " << std::endl;
        sleep(1);
    }
    // 等待其他线程
    for (int i = 0; i < nums; i++)
    {
        pthread_join(threads[i], nullptr);
    }
    return 0;
}